package com.cuiweiyou.bluemesh.ui;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.cuiweiyou.bluemesh.R;
import com.cuiweiyou.bluemesh.model.LightModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by macpro on 2018/10/24.
 */

public class DevicesCommAdapter extends RecyclerView.Adapter {

    private OnDeviceSelectedListener onDeviceSelectedListener;
    private List<LightModel> commBluetoothList;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_comm_tooth_content, null);
        return new BluetoothHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        LightModel light = commBluetoothList.get(position);
        String mile = light.getMile();
        String nickName = light.getNickname();

        BluetoothHolder holder = (BluetoothHolder) viewHolder;
        holder.macTextView.setText(nickName);
        holder.rssiTextView.setText(mile);

        if (light.isAdmi()) {
            holder.adminImageView.setVisibility(View.VISIBLE);
        } else {
            holder.adminImageView.setVisibility(View.INVISIBLE);
        }

        if (light.isOpen()) {
            holder.connectSwitch.setChecked(true);
        } else {
            holder.connectSwitch.setChecked(false);
        }

        if (light.isOnline()) {
            holder.lineImageView.setImageResource(R.mipmap.online);
        } else {
            holder.lineImageView.setImageResource(R.mipmap.offline);
        }
    }

    @Override
    public int getItemCount() {
        return null == commBluetoothList ? 0 : commBluetoothList.size();
    }

    public void updateDevices(List<LightModel> list) {
        if (null != commBluetoothList) {
            this.commBluetoothList.clear();
        }
        this.commBluetoothList = new ArrayList<>(list);

        notifyDataSetChanged();
    }

    class BluetoothHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private View deviceLayout;
        private ImageView adminImageView;
        private ImageView lineImageView;
        private TextView macTextView;
        private TextView rssiTextView;
        private Switch connectSwitch;

        public BluetoothHolder(View view) {
            super(view);

            deviceLayout = view.findViewById(R.id.deviceLayout);
            adminImageView = view.findViewById(R.id.adminImageView);
            lineImageView = view.findViewById(R.id.lineImageView);
            macTextView = view.findViewById(R.id.macTextView);
            rssiTextView = view.findViewById(R.id.rssiTextView);
            connectSwitch = view.findViewById(R.id.connectSwitch);

            deviceLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getLayoutPosition();
            LightModel model = commBluetoothList.get(position);
            if (null != onDeviceSelectedListener) {
                onDeviceSelectedListener.onDeviceSelected(model);
            }
        }
    }

    public void setOnDeviceSelectedListener(OnDeviceSelectedListener listener) {
        this.onDeviceSelectedListener = listener;
    }

    public interface OnDeviceSelectedListener {
        void onDeviceSelected(LightModel model);
    }
}
